﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Forms;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using GoldDigger.Models;
using GoldDigger.AppServices;
using GoldDigger.Data;

namespace GoldDigger.ViewModels
{
    class FeatureSelectionViewModel : ViewModelBase
    {
        private MainService _app;
        private ICommand _runFcbfCommand;
        private ICommand _returnCommand;
        private int _subsetSize;
        private double _threshold;
        private bool _useSize;
        private bool _useThreshold;
        private delegate void _fcbfParameters(string param1, double param2, Action<bool> param3);

        public FeatureSelectionViewModel(MainService ms)
        {
            _app = ms;
        }

        public int SubsetSize
        {
            get
            {
                return _subsetSize;
            }
            set
            {
                if (value.GetType() == typeof(int))
                {
                    _subsetSize = value;
                    RaisePropertyChanged("SubsetSize");
                }
            }
        }

        public double Threshold
        {
            get
            {
                return _threshold;
            }
            set
            {
                if (value.GetType() == typeof(double))
                {
                    _threshold = value;
                    RaisePropertyChanged("Threshold");
                }
            }
        }

        public bool UseSize
        {
            get
            {
                return _useSize;
            }
            set
            {
                _useSize = value;
                _useThreshold = !value;
                RaisePropertyChanged("UseSize");
                RaisePropertyChanged("UseThreshold");
                RaisePropertyChanged("CanRunAlgorithm");
            }
        }

        public bool UseThreshold
        {
            get
            {
                return _useThreshold;
            }
            set
            {
                _useThreshold = value;
                _useSize = !value;
                RaisePropertyChanged("UseThreshold");
                RaisePropertyChanged("UseSize");
                RaisePropertyChanged("CanRunAlgorithm");
            }
        }

        public ICommand RunFcbfCommand
        {
            get
            {
                return _runFcbfCommand ?? (_runFcbfCommand = new RelayCommand(FastCorrelationBasedFeature));
            }
        }

        public bool CanRunAlgorithm
        {
            get
            {
                if (UseSize || UseThreshold)
                    return true;
                else
                    return false;
            }
        }

        public void FastCorrelationBasedFeature()
        {
            Messenger.Default.Send(new NotificationMessage<bool>(true, "Wait"));
            _fcbfParameters delFcbf = new _fcbfParameters(_app.RunFcbf);
            if (_useSize)
            {
                delFcbf.BeginInvoke("SubsetSize", (double)SubsetSize, SelectionComplete, null, null);
            }
            else if (_useThreshold)
            {
                delFcbf.BeginInvoke("Threshold", Threshold, SelectionComplete, null, null);
            }
        }

        public void SelectionComplete(bool complete)
        {
            if (complete)
            {
                Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new FeatureSelectionResultsViewModel(_app), "ChangeVM"));
                Messenger.Default.Send(new NotificationMessage<bool>(false, "Wait"));
            }
        }

        public ICommand ReturnCommand
        {
            get
            {
                return _returnCommand ?? (_returnCommand = new RelayCommand(ReturnToDataSummary));
            }
        }

        public void ReturnToDataSummary()
        {
            Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new DataSummaryViewModel(_app), "ChangeVM"));
        }
    }
}
